package com.hfqby.camera.module.controller;

import com.hfqby.camera.common.utils.AssertUtils;
import com.hfqby.camera.common.utils.PreviewUtil;
import com.hfqby.camera.module.entity.vo.AjaxResult;
import com.hfqby.camera.module.entity.vo.LoginVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 海康摄像头业务层
 *
 * @author qiu bo yang
 * @date 2023-08-09 15:05
 */
@Controller
@RequestMapping("/hkCamera")
public class HkCameraController {


    @GetMapping("/index")
    public String index() {
        return "index";
    }

    /**
     * 登录并开启预览
     */
    @PostMapping("/loginAndPlayView")
    @ResponseBody
    public AjaxResult loginAndPlayView(@RequestBody LoginVo loginVo) {
        AssertUtils.notNull(loginVo.getIp(), "ip地址必填");
        AssertUtils.notNull(loginVo.getUsername(), "用户名必填");
        AssertUtils.notNull(loginVo.getPassword(), "密码必填");
        AssertUtils.notNull(loginVo.getPort(), "端口必填");
        AssertUtils.notNull(loginVo.getChannelId(),"通道号必填");
        return AjaxResult.success(PreviewUtil.loginAndPlayView(loginVo));
    }

    /**
     * 退出预览和登录
     */
    @GetMapping("/logoutPlayView")
    @ResponseBody
    public AjaxResult logoutPlayView(String userId, String playId) {
        AssertUtils.notNull(userId, "登录id必填");
        if (playId != null) {
            PreviewUtil.logoutPlayView(userId, playId);
        } else {
            PreviewUtil.logoutPlayView(userId);
        }
        return AjaxResult.success();
    }


    /**
     * 登录(判断是否在线)，只登录不预览
     * ---支持批量登录
     */
    @PostMapping("/login")
    @ResponseBody
    public AjaxResult login(@RequestBody List<LoginVo> loginVoList) {
        if (loginVoList.size() == 0) {
            return AjaxResult.error("登录信息必填");
        }
        return AjaxResult.success(PreviewUtil.loginCamera(loginVoList));
    }

    /**
     * 预览某个摄像头
     */
    @PostMapping("/playView")
    @ResponseBody
    public AjaxResult playView(@RequestBody LoginVo loginVo) {
        AssertUtils.notNull(loginVo.getUserId(), "登录id必填");
        AssertUtils.notNull(loginVo.getPlayId(), "预览id必填");
        return AjaxResult.success(PreviewUtil.playViewCamera(loginVo));
    }

    /**
     * 只关闭某个摄像头预览，但不退出登录
     */
    @PostMapping("/logoutPlayViewOnly")
    @ResponseBody
    public AjaxResult logoutPlayViewOnly(@RequestBody LoginVo loginVo) {
        AssertUtils.notNull(loginVo.getUserId(), "登录id必填");
        AssertUtils.notNull(loginVo.getPlayId(), "预览id必填");
        return AjaxResult.success(PreviewUtil.logoutPlayViewOnly(loginVo));
    }

}
